package cn.tx.controller;

import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;

import org.apache.commons.beanutils.BeanUtils;

import cn.tx.model.Product;
import cn.tx.model.ProductType;
import cn.tx.model.Supplier;
import cn.tx.query.ProductTypeQuery;
import cn.tx.service.ProductTypeService;
import cn.tx.service.SupplierService;
import cn.tx.util.JSONWriter;
import cn.tx.util.Page;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ModelDriven;

public class ProductTypeAction extends BaseAction implements ModelDriven<ProductType>{
	
	private ProductType productType = new ProductType();
	
	private ProductTypeService productTypeService;
	
	private SupplierService supplierService;
	
	
	
	
	public SupplierService getSupplierService() {
		return supplierService;
	}

	public void setSupplierService(SupplierService supplierService) {
		this.supplierService = supplierService;
	}

	public ProductType getProductType() {
		return productType;
	}

	public void setProductType(ProductType productType) {
		this.productType = productType;
	}

	/**
	 * 查询对象
	 */
	private ProductTypeQuery erpQuery = new ProductTypeQuery();
	
	
	public ProductTypeService getProductTypeService() {
		return productTypeService;
	}

	public void setProductTypeService(ProductTypeService productTypeService) {
		this.productTypeService = productTypeService;
	}
	

	public ProductTypeQuery getErpQuery() {
		return erpQuery;
	}

	public void setErpQuery(ProductTypeQuery erpQuery) {
		this.erpQuery = erpQuery;
	}

	/**
	 * 查询员工的列表
	 * @return
	 */
	public String productType_list(){
		//获得action的容器
		ActionContext context = ActionContext.getContext();
		Page page = productTypeService.selectObjectByConditionPage(erpQuery, exclude);
		List<Supplier> suppliers = supplierService.listAll();
		context.put("page", page);
		context.put("suppliers", suppliers);
		
		return SUCCESS;
	}
	
	/**
	 * 跳转到添加页面
	 * @return
	 */
	public String productType_input(){
		//获得action的容器
		ActionContext context = ActionContext.getContext();
		List<Supplier> suppliers = supplierService.listAll();
		context.put("suppliers", suppliers);
		return SUCCESS;
	}
	/**
	 * 跳转到修改页面
	 * @return
	 */
	public String productType_preupdate(){
		productType = productTypeService.getObjectById(productType.getProductTypeId());
		return SUCCESS;
	}
	
	
	public void productType_loadpro(){
		if(productType.getProductTypeId()!= null){
			ProductType type = productTypeService.getObjectById(productType.getProductTypeId());
			Set<Product> pset = type.getPset();
			JSONWriter.writeJSONArray(pset, response,new String[]{"productType"});
		}else{
			JSONWriter.writeJSONArray(new ArrayList(), response,new String[]{"productType"});
		}
	}
	/**
	 * 保存
	 * @return
	 * @throws IOException 
	 */
	public void productType_save() throws IOException{
		try {
			//把不同类的相同属性的值拷贝到目标对象
			BeanUtils.copyProperties(erpQuery, productType);
		} catch (IllegalAccessException | InvocationTargetException e) {
			e.printStackTrace();
		}
		List<ProductType> list = productTypeService.queryProductTypeBySupName(erpQuery);
		if(list.size() > 0){
			response.getWriter().write("no");
		}else{
			productTypeService.save(productType);
			response.getWriter().write("yes");
		}
		
		
	}
	/**
	 * 修改
	 * @return
	 */
	public String productType_update(){
		productTypeService.update(productType);
		return TOLIST;
	}
	/**
	 * 修改
	 * @return
	 */
	public void productType_delete(){
		productType = productTypeService.getObjectById(productType.getProductTypeId());
		Set<Product> pset = productType.getPset();
		
		if(pset.size() > 0){
			JSONWriter.writeString(NO, response);
		}else{
			productTypeService.delete(productType);
			JSONWriter.writeString(YES, response);
		}
		
		
	}

	@Override
	public ProductType getModel() {
		return productType;
	}
	
	
	
	

}
